import LaunchRamp.SecRole
import LaunchRamp.SecUser
import LaunchRamp.SecUserSecRole

class BootStrap {


	def init = { servletContext ->
		def springSecurityService

		def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
		def vendorRole = SecRole.findByAuthority('ROLE_VENDOR') ?: new SecRole(authority: 'ROLE_VENDOR').save(failOnError: true)
		
		def normalUser = SecUser.findByUsername('user') ?: new SecUser(
				username: 'user',
				password: 'user',
				enabled: true).save(failOnError:true)

		if (!normalUser.authorities.contains(userRole)) {
			SecUserSecRole.create normalUser, userRole
		}


		def adminUser = SecUser.findByUsername('admin') ?: new SecUser(
				username: 'admin',
				password:'admin',
				enabled: true).save(failOnError: true)

		if (!adminUser.authorities.contains(adminRole)) {
			SecUserSecRole.create adminUser, adminRole
		}
		
		def vendorUser = SecUser.findByUsername('vendor') ?: new SecUser(
			username: 'vendor',
			password: 'vendor',
			enabled: true).save(failOnError: true)
			
			if(!vendorUser.authorities.contains(vendorRole)) {
				SecUserSecRole.create vendorUser, vendorRole
			}
	}
	def destroy = {
	}
}
